clear all
set more off
cd "/Users/yasenov/Dropbox (IPL)/RefugeeBan/data/"

use "WRAPS/wraps_fips.dta"
keep if inlist(year, 2010, 2011, 2015, 2016, 2017, 2018)

merge 1:1 fips year using "crime/crime.dta", keep(1 3) keepusing(property violent)
drop if _merge == 1    
drop _merge

merge 1:1 fips year using "demographics/demographics.dta", keep(1 3) keepusing(pop_total s_black s_hisp s_white s_hs_dropout s_unempl s_not_in_lf s_hs_grad)
drop if _merge == 1  
drop _merge

gen lproperty_crime = log(property_crime + 1)
gen rproperty_crime = (property_crime / pop_total) * 100000

gen lviolent_crime = log(violent_crime + 1)
gen rviolent_crime = (violent_crime / pop_total) * 100000

gen lrefugees = log(refugees + 1)
gen rrefugees = (refugee / pop_total) * 100

sum

****************
****************

recode year (2011 = 2010) (2015 = 2016) (2018 = 2017)
tab year, m

collapse lproperty rproperty rviolent lviolent rrefugees lrefugees pop_total refugees property violent s_black s_hisp s_white s_hs_dropout s_unempl s_not_in_lf s_hs_grad, by(year fips)

****************
****************

reshape wide lproperty rproperty rviolent lviolent rrefugees lrefugees pop_total refugees property violent s_black s_hisp s_white s_hs_dropout s_unempl s_not_in_lf s_hs_grad, i(fips) j(year)

gen d_10_16_rproperty_crime = rproperty_crime2016 - rproperty_crime2010
gen d_10_16_rviolent_crime = rviolent_crime2016 - rviolent_crime2010

gen d_10_16_lproperty_crime = lproperty_crime2016 - lproperty_crime2010
gen d_10_16_lviolent_crime = lviolent_crime2016 - lviolent_crime2010

gen d_16_17_rrefugees = (rrefugees2017 - rrefugees2016) // * 100         // careful!!
gen d_16_17_lrefugees = lrefugees2017 - lrefugees2016

gen county_fips = substr(fips, 3, 3)
gen state_fips = substr(fips, 1, 2)

replace pop_total2016 = log(pop_total2016)

drop *2017 *2016

**************
**************
**************

global options cluster(state_fips)
global weight 
global controls  

global if1 if abs(d_10_16_rproperty_crime) <= 1000 
global if2 if abs(d_10_16_rviolent_crime) <= 1000 
global if3 if abs(d_10_16_lproperty_crime) <= 1 
global if4 if abs(d_10_16_lviolent_crime) <= 1 

label var d_16_17_rrefugees "$\Delta$\text{refugees per capita$^{\text{pre-post}}$}"
label var d_16_17_lrefugees "$\Delta$\text{log(refugees)}$^{\text{pre-post}}$"

eststo clear
eststo: reg d_10_16_rproperty_crime d_16_17_rrefugees $controls $if1 $weight, $options
estadd local stateFE ""
estadd ysumm
eststo: areg d_10_16_rproperty_crime d_16_17_rrefugees $controls $if1 $weight, $options absorb(state_fips)
estadd local stateFE "X"
estadd ysumm
eststo: reg d_10_16_rviolent_crime d_16_17_rrefugees $controls $if2 $weight, $options
estadd local stateFE ""
estadd ysumm
eststo: areg d_10_16_rviolent_crime d_16_17_rrefugees $controls $if2 $weight, $options absorb(state_fips)
estadd local stateFE "X"
estadd ysumm

eststo: reg d_10_16_lproperty_crime d_16_17_lrefugees $controls $if3 $weight, $options
estadd local stateFE ""
estadd ysumm
eststo: areg d_10_16_lproperty_crime d_16_17_lrefugees $controls $if3 $weight, $options absorb(state_fips)
estadd local stateFE "X"
estadd ysumm
eststo: reg d_10_16_lviolent_crime d_16_17_lrefugees $controls $if4 $weight, $options
estadd local stateFE ""
estadd ysumm
eststo: areg d_10_16_lviolent_crime d_16_17_lrefugees $controls $if4 $weight, $options absorb(state_fips)
estadd local stateFE "X"
estadd ysumm

esttab, label ///
	keep(d_16_17_rrefugees d_16_17_lrefugees) b(%4.3f) se(%4.3f) ///
	mtitles("Property" "Property" "Violent" "Violent" "Property" "Property" "Violent" "Violent") ///
	mgroups("Crime Rates" "Log Number of Crimes", pattern(1 0 0 0 1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(})span erepeat(\cmidrule(lr){@span})) ///	
	scalars("stateFE State Fixed Effects") ///
	star(* 0.05 ** 0.01 *** 0.001) 

esttab using "../results/tab_pretrends.tex", replace label ///
	keep(d_16_17_rrefugees d_16_17_lrefugees) b(%4.3f) se(%4.3f) ///
	mtitles("Property" "Property" "Violent" "Violent" "Property" "Property" "Violent" "Violent") ///
	mgroups("Crime Rates" "Log Number of Crimes", pattern(1 0 0 0 1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(})span erepeat(\cmidrule(lr){@span})) ///	
	scalars("stateFE State Fixed Effects") ///
	star(* 0.05 ** 0.01 *** 0.001) nonotes ///
	stats(stateFE N r2, label("State FE" "N" "R$^2$") fmt(%4.3f %3.0f %4.3f %4.3f)) 
